package cate.game.activity.specialtraining;

import cate.common.table.activity.specialtraining.card.SpecialTrainingCardSheet;
import cate.common.table.activity.specialtraining.point.SpecialTrainingPointSheet;
import cate.common.table.activity.specialtraining.shop.SpecialTrainingShopSheet;
import cate.common.table.activity.specialtraining.task.SpecialTrainingTaskSheet;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.specialtraining.msg.SpecialTrainingActivityInfoResp;
import cate.game.activity.specialtraining.po.SpecialTrainingActivityPointPO;
import cate.game.activity.specialtraining.po.SpecialTrainingActivityShopPO;
import cate.game.activity.specialtraining.po.SpecialTrainingActivityTaskPO;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

@NoteClass(value = "特训活动")
public class SpecialTrainingActivity extends RoleActivitySingle {
    @NoteField(value = "当前天数")
    public int day;
    @NoteField(value = "上次检测天数的时间")
    public long lastDayCheckTime;
    @NoteField(value = "任务")
    public SpecialTrainingActivityTaskPO taskPO;
    @NoteField(value = "商品")
    public SpecialTrainingActivityShopPO shopPO;
    @NoteField(value = "积分")
    public SpecialTrainingActivityPointPO pointPO;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (taskPO == null) {
            taskPO = new SpecialTrainingActivityTaskPO();
        }
        taskPO.initialize(this);
        if (shopPO == null) {
            shopPO = new SpecialTrainingActivityShopPO();
        }
        shopPO.initialize(this);
        if (pointPO == null) {
            pointPO = new SpecialTrainingActivityPointPO();
        }
        pointPO.initialize(this);
    }

    @Override
    public void onEnter() {
        super.onEnter();
        if (!enable()) {
            return;
        }
        long nowTime = game().time.now();
        if (TimeTool.isDifferentDay(lastDayCheckTime, nowTime)) {
            day = getCurrentDay();
            lastDayCheckTime = nowTime;
        }
    }

    @Override
    public void excelCheck() {
        taskPO.excelCheck();
        shopPO.excelCheck();
        pointPO.excelCheck();
    }

    public void beforeOver() {
        taskPO.beforeOver();
        shopPO.beforeOver();
        pointPO.beforeOver();
    }

    public void clear() {
        day = 0;
        lastDayCheckTime = 0;
        taskPO.clear();
        shopPO.clear();
        pointPO.clear();
    }

    public void noticeUpdate() {
        role.sendNow(new SpecialTrainingActivityInfoResp(this));
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        long nowTime = game().time.now();
        if (silence) {
            if (!TimeTool.isDifferentDay(lastDayCheckTime, nowTime)) {
                return;
            }
        }
        day = getCurrentDay();
        lastDayCheckTime = nowTime;
        taskPO.onDaySpan(silence);
        if (!silence) {
            noticeUpdate();
        }
    }

    @Override
    public void onRoleOpen() {

    }

    @Override
    public void onRoleEnd() {
        beforeOver();
        clear();
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    @Override
    public GameResult<Void> allowPay(PayItemRow payItemRow) {
        return shopPO.allowBuy(payItemRow.funcSubId);
    }

    @Override
    public void onPaySuccess(PayItemRow payItemRow) {
        shopPO.provideReward(payItemRow.funcSubId);
        noticeUpdate();
    }

    /**
     * 获取页签配置Sheet
     */
    @JsonIgnore
    public SpecialTrainingCardSheet getCardSheet() {
        switch (funcId()) {
            case GDFunc.ACT_SEVEN_DAY_SPECIAL_TRAINING:
                return role.getGame().table.sevenDaySpecialTrainingExcel.card;
            default:
                return null;
        }
    }

    /**
     * 获取任务配置Sheet
     */
    @JsonIgnore
    public SpecialTrainingTaskSheet getTaskSheet() {
        switch (funcId()) {
            case GDFunc.ACT_SEVEN_DAY_SPECIAL_TRAINING:
                return role.getGame().table.sevenDaySpecialTrainingExcel.task;
            default:
                return null;
        }
    }

    /**
     * 获取商品配置Sheet
     */
    @JsonIgnore
    public SpecialTrainingShopSheet getShopSheet() {
        switch (funcId()) {
            case GDFunc.ACT_SEVEN_DAY_SPECIAL_TRAINING:
                return role.getGame().table.sevenDaySpecialTrainingExcel.shop;
            default:
                return null;
        }
    }

    /**
     * 获取积分配置Sheet
     */
    @JsonIgnore
    public SpecialTrainingPointSheet getPointSheet() {
        switch (funcId()) {
            case GDFunc.ACT_SEVEN_DAY_SPECIAL_TRAINING:
                return role.getGame().table.sevenDaySpecialTrainingExcel.point;
            default:
                return null;
        }
    }

    /**
     * 获取积分道具ID
     */
    @JsonIgnore
    public byte getPointItemTid() {
        switch (funcId()) {
            case GDFunc.ACT_SEVEN_DAY_SPECIAL_TRAINING:
                return GDObj.Num.ST_POINT;
            default:
                return -1;
        }
    }

    /**
     * 获取操作类型
     */
    @JsonIgnore
    public int getGDOperation() {
        switch (funcId()) {
            case GDFunc.ACT_SEVEN_DAY_SPECIAL_TRAINING:
                return GDOperation.ACT_SEVEN_DAY_SPECIAL_TRAINING;
            default:
                return GDOperation.NULL;
        }
    }
}
